package com.woniuxy.profile.controller;


import com.woniuxy.commons.dto.ProfileDto;
import com.woniuxy.commons.entity.PageBean;
import com.woniuxy.commons.entity.Profile;
import com.woniuxy.commons.entity.ResponseEntity;
import com.woniuxy.profile.service.ProfileService;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author: dengzj
 * @Description:
 * @Date: Created in 14:31 2020/7/28
 */
@RestController
@RequestMapping("/profile")
public class ProfileController {
    @Autowired
    private ProfileService profileService;
    /*
    *@Author: dengzj
    *@Description: 添加用户档案
    *@Date: 17:30 2020/7/28
    */
    @RequestMapping("/add")
    public ResponseEntity<Void> addProfile(@RequestBody Profile profile) {
        System.out.println(profile);
        ResponseEntity responseEntity = new ResponseEntity();
        if (profile == null) {
            return responseEntity.setCode(300).setMessage("添加无效数据");
        }
            int state=profileService.insertProfile(profile);
            if (state!=1) {
                responseEntity.setCode(301).setMessage("系统走丢了");
            }
            responseEntity.setCode(200).setMessage("成功");
            return responseEntity;

    }
    /*
    *@Author: dengzj
    *@Description: 查询单个用户档案
    *@Date: 9:31 2020/7/29
    */
    @RequestMapping("/find/{Id}")
    public ResponseEntity findProfile(@PathVariable("Id") int uid){
        ResponseEntity responseEntity=new ResponseEntity();
        Profile findprofile=profileService.findProfileByUid(uid);
        System.out.println(findprofile);
        responseEntity.setCode(200).setMessage("成功").setData(findprofile);
        return responseEntity;

    }
    /*
    *@Author: dengzj
    *@Description: 修改用户档案
    *@Date: 9:33 2020/7/29
    */
    @RequestMapping("/update")
    public ResponseEntity<Void> updateProfile(@RequestBody Profile profile){
        System.out.println(profile);
        ResponseEntity responseEntity=new ResponseEntity();
        int state=profileService.updateProfile(profile);
        if (state!=1){
            return responseEntity.setData(301).setMessage("添加失败！");
        }
        return responseEntity.setCode(200).setMessage("修改成功！");
    }
    /*
    *@Author: dengzj
    *@Description: 删除单个用户档案
    *@Date: 9:41 2020/7/29
    */
    @RequestMapping("/delete")
    public ResponseEntity<Void> deleteProfile(int uid){
        ResponseEntity responseEntity=new ResponseEntity();
        int state=profileService.deleteProfile(uid);
        if (state!=1){
            return responseEntity.setCode(301).setMessage("删除失败！");
        }
        return responseEntity.setCode(200).setMessage("删除成功！");
    }

    
    /*
    *@Author: dengzj
    *@Description: 批量删除员工档案
    *@Date: 9:52 2020/7/29
    */
    @RequestMapping("/deletebatch")
    public ResponseEntity deleteBatch(@RequestBody int[] batch){

        ResponseEntity responseEntity=new ResponseEntity();
        int state=profileService.deleteProfileBatch(batch);
        if(state!=1){
            return responseEntity.setCode(301).setMessage("批量删除失败");
        }
        return responseEntity.setCode(200).setMessage("批量删除成功");
    }
    /*
    *@Author: dengzj
    *@Description: 多条件查询档案
    *@Date: 10:19 2020/8/1
    */
    @RequestMapping("/condition")
    @ResponseBody
    public ResponseEntity<PageBean<Profile>> findProfileByCondition(@RequestBody ProfileDto profileDto){
        System.out.println(profileDto);

        String p=profileDto.getP();
        int page=1;
        if (p!=null){
            page=Integer.parseInt(p);
        }
        ResponseEntity<PageBean<Profile>> entity=new ResponseEntity<>();
        PageBean<Profile> findprofile=profileService.findProfileByCondition(profileDto,page);
        if (findprofile==null){
           entity.setMessage("未找到");
           entity.setCode(301);
           return entity;
        }
        entity.setData(findprofile);
        entity.setCode(200);
        entity.setMessage("ok");
        return entity;
    }

}
